#!/bin/bash

# 钉钉机器人 Webhook URL
DINGTALK_WEBHOOK_URL="https://oapi.dingtalk.com/robot/send?access_token=a08237f5fb20f332657cc066022fb067bd1f2d6cd4b3db4651a2c16edbcc1251"

# Canal 日志文件路径数组
CANAL_LOG_PATHS=(
    "/data/canal-115-deployer-Adapter/canal-deployer-115/logs/canal/canal.log"
    "/data/canal_pay-1.1.5/canal-deployer-115/logs/canal/canal.log"
    "/data/canal_fk-1.1.5/canal.deployer-1.1.5/logs/canal/canal.log"
)

# 监控的关键字，表示同步异常
ERROR_KEYWORDS="ERROR|WARN|exception|同步失败|timeout|deadlock|connection refused|connection reset|retry|failed|aborted|rejected|invalid|cannot|unexpected|broken|corrupted"

# 获取当前日期
TODAY=$(date "+%Y-%m-%d")

# 记录已经通知过的异常日志的文件
NOTIFIED_LOG_FILE="/data/canal_notified_logs/$TODAY"

# 初始化已通知日志文件
touch "$NOTIFIED_LOG_FILE"

# 获取当前时间
CURRENT_TIME=$(date "+%Y-%m-%d %H:%M:%S")

# 遍历所有日志文件路径
for CANAL_LOG_PATH in "${CANAL_LOG_PATHS[@]}"; do
    # 检查 Canal 日志中是否有异常
    ALL_ERROR_LOGS=$(grep -E "$ERROR_KEYWORDS" "$CANAL_LOG_PATH")
    # 过滤出当天的异常日志
    TODAY_ERROR_LOGS=$(echo "$ALL_ERROR_LOGS" | grep -E "$TODAY")
    # 过滤出非当天的异常日志
    NON_TODAY_ERROR_LOGS=$(echo "$ALL_ERROR_LOGS" | grep -Ev "$TODAY")

    # 处理非当天的异常日志，只记录到文件中
    while IFS= read -r line; do
        if ! grep -qF "$line" "$NOTIFIED_LOG_FILE"; then
            echo "$line" >> "$NOTIFIED_LOG_FILE"
        fi
    done <<< "$NON_TODAY_ERROR_LOGS"

    # 处理当天的异常日志
    NEW_TODAY_ERROR_LOGS=""
    while IFS= read -r line; do
        if ! grep -qF "$line" "$NOTIFIED_LOG_FILE"; then
            NEW_TODAY_ERROR_LOGS="$NEW_TODAY_ERROR_LOGS$line"$'\n'
            echo "$line" >> "$NOTIFIED_LOG_FILE"
        fi
    done <<< "$TODAY_ERROR_LOGS"

    # 只取最后三条异常日志
    NEW_TODAY_ERROR_LOGS=$(echo "$NEW_TODAY_ERROR_LOGS" | tail -n 3)

    if [ -n "$NEW_TODAY_ERROR_LOGS" ]; then
        # 如果有新的当天异常，发送钉钉告警
        MESSAGE="【Canal 数据同步异常告警】\n\n时间：$CURRENT_TIME\n\n日志路径：$CANAL_LOG_PATH\n\n异常日志信息：\n$NEW_TODAY_ERROR_LOGS\n\n请及时处理！"
        
        # 使用 curl 发送消息到钉钉
        curl -s "$DINGTALK_WEBHOOK_URL" \
            -H 'Content-Type: application/json' \
            -d "{
                \"msgtype\": \"text\",
                \"text\": {
                    \"content\": \"$MESSAGE\"
                }
            }"
    else
        echo "【Canal 数据同步正常】时间：$CURRENT_TIME 路径：$CANAL_LOG_PATH"
    fi
done